var winW = $(window).width(),
	winH = $(window).height(),
	isloading = true;

$(document).ready(function(){
	var speed = 5,
		rotate_c = 0,
		rotate_lr = 360;
		
	function setrotate_z(value,value_c){
		$('#loading .rotate .circle_r').css({
			"-webkit-transform":"rotateZ("+ value +"deg)",
			"-moz-transform":"rotateZ("+ value +"deg)",
			"-ms-transform":"rotateZ("+ value +"deg)",
			"-o-transform":"rotateZ("+ value +"deg)",
			"transform":"rotateZ("+ value +"deg)"
		});
		$('#loading .rotate .circle_l').css({
			"-webkit-transform":"rotateZ("+ value +"deg)",
			"-moz-transform":"rotateZ("+ value +"deg)",
			"-ms-transform":"rotateZ("+ value +"deg)",
			"-o-transform":"rotateZ("+ value +"deg)",
			"transform":"rotateZ("+ value +"deg)"
		});
		$('#loading .rotate .circle_c').css({
			"-webkit-transform":"rotateZ("+ value_c +"deg)",
			"-moz-transform":"rotateZ("+ value_c +"deg)",
			"-ms-transform":"rotateZ("+ value_c +"deg)",
			"-o-transform":"rotateZ("+ value_c +"deg)",
			"transform":"rotateZ("+ value_c +"deg)"
		});
	}
	function circle(){
		if(isloading){
			var num = Math.round(Math.random()*3+7);
			if(rotate_c < 360){
				setrotate_z("",rotate_c);
				rotate_c += speed;
			}else if(rotate_c >= 360){
				rotate_c = 0;
			}
			if(rotate_lr){
				setrotate_z(rotate_lr);
				rotate_lr -= speed;
			}else if(rotate_lr <= 0){
				rotate_lr = 360;
			}
			setTimeout(function(){
				circle();
				$('#loading .loadbg').css({"opacity":num/10});
			},50);
		}
	}
	circle();
	
	var img = new Image();		
	img.src = "./images/load_w.png";
	
	if(img.complete){
		imageLoaded();
	}else{
		img.onload = imageLoaded();
	};
	
	function imageLoaded(){
		$('#loading .loadingwarp').show();
	}
	
	
		
});




$('.mini_nav').on("click", function(){
	if($('.menu').is(":visible")){
		$('.menu').slideUp("slow");
		$(this).removeClass('open');
	}else{
		$(this).addClass('open');
		$('.menu').slideDown("slow");
	}
});

$('.menuwarp .m_l').on("click", function(){
	if($(this).find('ul').is(":visible")){
		$(this).find('ul').hide();
	}else{
		$(this).find('ul').show();
	}
});



if($('body').attr("class") == "fullpage"){

	
}



// 以下是拖动效果
var startX = 0,
    startY = 0;
    margin = 0;
var pages = null;
var curPage = 0,
	maxPage = $(".sec > div").length-1;
var pageWidth = 0,
    pageHeight = 0;
var lineHeight = 0, secHeight = 0;
var targetElement = null;
var scrollPrevent = false, movePrevent = false, touchDown = false, mousePrevent = false, isAnimating = false, wheelPrevent = true;
var animationTimeout;



function initPage(){
	pageWidth = winW;
	pageHeight = winH;
	pages = $(".sec > div");
	

    $(".sec > div").css({
        "width":"100%",//"height":$(".wrap").height()+"px"
        "height":pageHeight+"px"
    });
	
	$(".secWarp").css({
        "height":pageHeight+"px"
    });
    //$(".wrap").height($(".wrap").height());

    secHeight = pageHeight * $(".sec > div").length;
    lineHeight = 832 * secHeight / pageHeight;

    //animatePage(curPage);
    //$(".sec, .line").removeClass("drag");

}

function onStart (e) {
    if(movePrevent == true){
        event.preventDefault();
        return false;
    }
    if($(e.target).parents("#container").length==1){
        scrollPrevent = true;
    }else{
        scrollPrevent = false;
    }

    if(!$(e.target).parents("#container").length==1){
	    $(".map_show").removeClass("show");
    }

    touchDown = true;

    // 起始点，页面位置
    startX = e.pageX;
    startY = e.pageY;
    //margin = parseInt($(".sec").css("top"));
    //-webkit-transform:translateY(0px)

    //matrix(1, 0, 0, 1, 0, 8)

    $(".sec, .line").addClass("drag");

    margin = $(".sec").css("-webkit-transform");
    //margin = "matrix(1, 0, 0, 1, 0, -50)";
    margin = margin.replace("matrix(", "");
    margin = margin.replace(")", "");
    margin = margin.split(",");
    margin = parseInt(margin[5]);
}

function onMove (e, oe) {
    if(movePrevent == true || touchDown != true){
        event.preventDefault();
        return false;
    }
    event.preventDefault();
    if( scrollPrevent==false && e.pageY!=startY){
        var temp = margin + e.pageY - startY;
        $(".sec").css("-webkit-transform", "matrix(1, 0, 0, 1, 0, "+temp+")");
    }
}

function onEnd (e) {
    if(movePrevent == true){
        event.preventDefault();
        return false;
    }

    touchDown = false;

    if( scrollPrevent==false ){
        // 抬起点，页面位置
        endX = e.pageX;
        endY = e.pageY;
        // swip 事件默认大于50px才会触发，小于这个就将页面归回
        if( Math.abs(endY-startY)<=50) {
            animatePage(curPage);
        }else{
        	if(endY>startY){
        		prevPage();
        	}else{
        		nextPage();
        	}
        }
    }

}

function prevPage(){
	if(isAnimating) return;
    var newPage = curPage - 1;
    animatePage(newPage);
    
}
function nextPage(){
	if(isAnimating) return;
    var newPage = curPage + 1;
    animatePage(newPage);
}

function animatePage( newPage ){
    if(newPage<0){
        newPage = 0;
    }
    if(newPage>$(".sec > div").length-1){
        newPage = $(".sec > div").length-1;
    }
	
	
	/*switch(newPage){
		case 1:
			$('.menu_pro .bg01').show();
		break;
		default:
			
	}*/
	//console.log("newPage:"+newPage+"=====curPage:"+curPage)
	
	
    curPage = newPage;
	
	console.log("-----------maxPage:"+maxPage+"------curPage:"+curPage)
	
	setrightNav(curPage);
	if(curPage == 0){
		$('.rightNav').hide();
	}else{
		$('.rightNav').show();
	}
	
	if($('body').width() < 750){
		if(curPage == maxPage){
			$('.bottom_con').hide();
		}else{
			$('.bottom_con').show();
		}
	}
	
    var newMarginTop = newPage * (-pageHeight);
    $(".sec").css({
        "-webkit-transform" : "matrix(1, 0, 0, 1, 0, "+newMarginTop+")",
		"-moz-transform" : "matrix(1, 0, 0, 1, 0, "+newMarginTop+")",
		"-ms-transform" : "matrix(1, 0, 0, 1, 0, "+newMarginTop+")",
		"transform" : "matrix(1, 0, 0, 1, 0, "+newMarginTop+")"
    });


    movePrevent = true;
	isAnimating = true;
	setTimeout(function(){movePrevent=false; isAnimating=false;},300 );
	
	

    // 每页动画
    if( !$(pages[curPage]).hasClass("sec0" + (curPage+1) + "_show") ){
        $(pages[curPage]).addClass("sec0" + (curPage+1) + "_show");
    }
    $(pages[curPage-1]).removeClass("sec0" + (curPage) + "_show");
    $(pages[curPage+1]).removeClass("sec0" + (curPage+2) + "_show");

    
}
function gotoPage(newPage){
    animatePage(newPage);
}


//set animation timeout
function setAnimationTimeout(){
	mousePrevent = true;
	clearTimeout(animationTimeout);
	animationTimeout = setTimeout("mousePrevent = false;", 1100);
}


//init mouse wheel and bind event
function objmousewheel(){
	$(document).mousewheel(function(event, delta) {
		
		
		if(curPage == maxPage){
			hidebottom_con();
		}else if( curPage < maxPage){
			showbottom_con();
		}
		
		
		if(isAnimating) return false;
		
		if( event.deltaY < 0){
			if(!mousePrevent && curPage < maxPage){
				nextPage();
				setAnimationTimeout();
			}else if(curPage == maxPage){
				wheelPrevent = false;
				$('.rightNav').hide();
				return;
			}
		}else if(event.deltaY > 0){
			if(!mousePrevent && wheelPrevent){
				prevPage();
				setAnimationTimeout();
			}else{
				return;
			}
		}
		
		
		
		return false;
	
	});
}

function gonextPage(){
	if(curPage >= maxPage){
		var topn = $('.ind_itmes').offset().top;
		$('html, body').animate({scrollTop:topn},300);
		hidebottom_con();
		$('.rightNav').hide();
	}else{
		nextPage();
	}
	//nextPage();
}

function goprevPage(){
	wheelPrevent = true;
	$('html, body').animate({scrollTop:0},100);
	$('.rightNav').show();
	showbottom_con();
}


function hidebottom_con(){
	$('.bottom_con').hide();
	$('.bottom_conUp').show();
}

function showbottom_con(){
	$('.bottom_con').show();
	$('.bottom_conUp').hide();
}



function initKeyboard(){
	$(document).keydown(function(e){
		e.preventDefault();
	    switch(e.which) {
		    case 38: // up
				//scrollPrevPage();
		    break;

		    case 40: // down
			    //scrollNextPage();
		    break;

		    default: return; // exit this handler for other keys
	    }
    });
}



function setrightNav(value){
	var obj = $('.rightNav'),
		val = value || "0";
	if((val+1)>obj.find('li').length){
		val = obj.find('li').length-1;
	}
	obj.find('a').removeClass("active");
	obj.find('li').eq(val).find('a').addClass('active');
	obj.css({"margin-top":(-obj.height()*0.5)+"px"});
}






function resize() {
	$(window).resize(function () {
		winW = $(window).width();
		winH = $(window).height();
		if($('body').width() > 750){
			setTimeout(function(){$('header .menu').attr('style','');$('.mini_nav').removeClass('open');},300);
		}
		initPage();
		setrightNav();
	});
}

function gotop(){
	$('html, body').animate({scrollTop:0},300);
}


function gotopFooter(){
	$('html, body').animate({scrollTop:0},300);
	if($('body').attr("class") == "fullpage"){
		wheelPrevent = true;
		gotoPage(0);
		showbottom_con();
	}
}




//////

var isBig = false;

function setbp(value,num){
	if($('body').width() < 750){
		return;
	}
	var obj = $('.'+value);
	isBig = true;
	$('.bigPic').show();
	obj.parent().find('img').hide();
	obj.parent().parent().find('li').removeClass('active');
	obj.show();
	obj.parent().parent().find('li').eq(num).addClass('active');
	
	picRun(value);
	
	
}
function picRun(value){
	
	var mouseX = event.clientX,
    	mouseY = event.clientY,
		obj = $('.'+value),
		imgW = obj.width(),
		imgH = obj.height(),
		mar_l = "";
	function prun(){
		if(isBig){
			obj.mousemove(function(e) {  
				mouseX = e.pageX;
				mouseY = e.pageY;
			 
				if(obj.width()<winW){
					mar_l = "";
				}else{
					mar_l = -(imgW - winW)*(mouseX/winW);
				}
				if(obj.height()<winH){
					mar_t = (winH-imgH)*0.5;
				}else{
					mar_t = -(imgH-winH)*(mouseY/winH);
				}
				obj.css({
					"margin-top":mar_t+"px",
					"margin-left":mar_l+"px"
				});
				
			});
		}
	}
	prun();
	
}

function closebp(){
	isBig = false;
	$('.bigPic').hide();
}



///////
window.onload = function(){
	resize();
	setTimeout(function(){
		isloading = false;
		$('#loading').hide();
	},30);
	
	initPage();
	setrightNav();
	
	if($('body').attr("class") == "fullpage"){
		$('html, body').animate({scrollTop:0},10);
		objmousewheel();
	}
	
	if(curPage == 0){
		$('.rightNav').hide();
	}else{
		$('.rightNav').show();
	}
	
};
